package com.wing.wcabinet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.order.service.DynamicService;
import com.wing.wcabinet.dao.WcabCellAllocationDao;
import com.wing.wcabinet.model.entity.WcabCellAllocation;
import com.wing.wcabinet.model.entity.WcabCellAllocationDetail;
import com.wing.wcabinet.model.entity.WcabCellInventoryInoutDetail;
import com.wing.wcabinet.model.request.WcabCellAllocationForm;
import com.wing.wcabinet.model.request.WcabCellInventoryInoutDetailForm;
import com.wing.wcabinet.model.request.WcabCellInventoryInoutForm;
import com.wing.wcabinet.model.response.WcabCellAllocationVO;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 仓库调拨-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class WcabCellAllocationServiceImpl extends ServiceImpl<WcabCellAllocationDao, WcabCellAllocation> implements WcabCellAllocationService {

    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private WcabCellInventoryInoutDetailService cellInventoryInoutDetailService;

    @Autowired
    private WcabCellAllocationDetailService wcabCellAllocationDetailService;

    @Override
    public int add(WcabCellAllocationForm wcabCellAllocationForm) {

        //校验

        WcabCellAllocation wcabCellAllocation =new WcabCellAllocation();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(WcabCellAllocation.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(wcabCellAllocationForm, wcabCellAllocation, copyOptions);

        return baseMapper.insert(wcabCellAllocation);
    }

    @Override
    public List<WcabCellAllocationVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<WcabCellAllocationVO> pageList4app(Page page, Map<String, Object> params) {
        List<WcabCellAllocationVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<WcabCellAllocationVO> pageList(Page page, Map<String, Object> params) {
        List<WcabCellAllocationVO> list = baseMapper.list(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public WcabCellAllocationVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional
    public void allocate(WcabCellInventoryInoutForm cellInventoryInoutForm) {
        if (CollectionUtil.isEmpty(cellInventoryInoutForm.getDetailList())){
            throw new BusinessException("缺少调拨明细参数");
        }
        Long creator= JwtUtils.getLoginUserId();
        String creatorName=JwtUtils.getLoginUsername();
        WcabCellAllocation wcabCellAllocation=new WcabCellAllocation();
//        BeanUtils.copyProperties(wcabCellInventoryInoutForm,wcabCellAllocation);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");

        String code=dynamicService.getDayNextCode("wcab_cell_allocation");
        String day=simpleDateFormat.format(new Date());
        wcabCellAllocation.setCode("AL-"+day+"-"+code);
        wcabCellAllocation.setDay(Integer.valueOf(day));
        wcabCellAllocation.setNo(Integer.valueOf(code));
        wcabCellAllocation.setCreator(creator);
        wcabCellAllocation.setCreatorName(creatorName);
//        wcabCellAllocation.setInoutType(WcabCellInventoryInout.InoutType.in);
//        wcabCellAllocation.setBusinessType(WcabCellInventoryInout.BusinessType.normal);
//        wcabCellAllocation.setOperationType(WcabCellInventoryInout.OperationType.jsonCreator(wcabCellLoadForm.getOperationType().name()));

        wcabCellAllocation.setCabCode(cellInventoryInoutForm.getCabCode());
        wcabCellAllocation.setCategoryId(cellInventoryInoutForm.getCategoryId());

        baseMapper.insert(wcabCellAllocation);
        for (WcabCellInventoryInoutDetailForm detailForm:cellInventoryInoutForm.getDetailList()){
            WcabCellAllocationDetail allocationDetail=new WcabCellAllocationDetail();
            BeanUtils.copyProperties(detailForm,allocationDetail);
            allocationDetail.setAllocationId(wcabCellAllocation.getId());
            wcabCellAllocationDetailService.save(allocationDetail);
        }
        cellInventoryInoutForm.setAllocationId(wcabCellAllocation.getId());
        cellInventoryInoutDetailService.inventoryOutIn(cellInventoryInoutForm);
    }
}
